﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Website.Models;
using Business.DAO;
using Business.Models;

namespace Website.Controllers
{
    public class MonHocController : Controller
    {
        MonHocDAO dao = new MonHocDAO();
        IEnumerable<MonHoc> model = null;

        int IdTruongHoc { get; set; }

        private bool CheckLogin()
        {
            try
            {
                QuanTri quanTri = Utils.CheckLogin_QuanTri();
                if (quanTri != null)
                {
                    IdTruongHoc = quanTri.NhanVien.IdTruongHoc;
                    ViewBag.IdTruongHoc = IdTruongHoc;
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Website.Models.Utils.RoleException ex)
            {
                throw ex;
            }
        }

        public ActionResult Index(int IdMonHoc = 0, String Function = "")
        {
            if (CheckLogin())
            {

                ViewBag.Function = Function;

                if (Function != "" && Function == "Delete" && IdMonHoc != 0)
                {
                    ViewBag.DeleteResult = dao.Delete(IdMonHoc);
                    //Tra form lai dang Create Form
                    ViewBag.Function = "Create";
                }

                model = dao.GetAll();
                return View(model);
            }
            else
            {
                return RedirectToAction("Index", "Home");
            }
        }

        [HttpPost]
        public ActionResult Index(MonHoc daiLy, String Function = "")
        {
            ViewBag.Function = Function;

            if (Function == "Edit")
            {
                Edit(daiLy);
            }
            else if (Function == "Create")
            {
                Create(daiLy);
            }

            model = dao.GetByTruongHoc(IdTruongHoc);
            return View(model);
        }

        public ActionResult Create(int IdTruongHoc, int IdDaiLy = 0)
        {
            ViewBag.IdTruongHoc = IdTruongHoc;
            return View();
        }

        private void Create(MonHoc daiLy)
        {
            try
            {
                ViewBag.CreateResult = dao.Create(daiLy);
            }
            catch (Exception ex)
            {
            }
        }

        public ActionResult GridViewPartial()
        {
            try
            {
                model = dao.GetByTruongHoc(IdTruongHoc);
            }
            catch (Exception ex)
            {
            }
            return PartialView("GridView", model);
        }

        public ActionResult Edit(int IdDaiLy)
        {
            MonHoc daiLy = null;
            try
            {

                if (IdDaiLy != 0)
                {
                    model = dao.GetByTruongHoc(IdTruongHoc);
                    daiLy = model.Where(daily => daily.IdMonHoc == IdDaiLy).First();
                }
                else
                {
                    daiLy = new MonHoc();
                }
            }
            catch (Exception ex)
            {
            }
            return View(daiLy);
        }

        private void Edit(MonHoc daiLy)
        {
            try
            {
                if (daiLy.IdMonHoc != 0)
                {
                    ViewBag.EditResult = dao.Edit(daiLy);
                }
            }
            catch (Exception ex)
            {
            }
        }
    }
}